package com.lw.obj;

import com.lw.GameWin;
import com.lw.config.GameState;
import com.lw.utils.GameUtils;
import lombok.Data;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;

/**
 * 蛇头
 */
@Data
public class HeadObj extends GameObj {
    /**
     * 方向 up down left right
     */
    private String direction = "right";

    public HeadObj(int x, int y, GameWin gameWin) {
        this(GameUtils.rightImg, x, y, gameWin);
    }

    public HeadObj(Image img, int x, int y, GameWin gameWin) {
        super(img, x, y, gameWin);

        //添加键盘监听
        this.gameWin.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                changeDirection(e);
            }
        });
    }

    /**
     * 控制移动方向
     * a left, d right, w up, s down
     */
    public void changeDirection(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_A -> {
                //按下A键，朝左运动，需要判断目前不是朝右，不能反方向运动
                if (!"right".equals(direction)) {
                    direction = "left";
                    img = GameUtils.leftImg;
                }
            }
            case KeyEvent.VK_D -> {
                if (!"left".equals(direction)) {
                    direction = "right";
                    img = GameUtils.rightImg;
                }
            }
            case KeyEvent.VK_W -> {
                if (!"down".equals(direction)) {
                    direction = "up";
                    img = GameUtils.upImg;
                }
            }
            case KeyEvent.VK_S -> {
                if (!"up".equals(direction)) {
                    direction = "down";
                    img = GameUtils.downImg;
                }
            }
        }
    }

    /**
     * 移动
     */
    public void move() {
        //蛇身移动
        List<BodyObj> bodyObjList = gameWin.bodyObjList;
        for (int i = bodyObjList.size()-1; i >0 ; i--) {//此处蛇身的移动就是上一节身体所在位置
            bodyObjList.get(i).x = bodyObjList.get(i - 1).x;
            bodyObjList.get(i).y = bodyObjList.get(i - 1).y;


            //判断蛇头和蛇身的碰撞判断
            if(GameUtils.isCollision(this,bodyObjList.get(i))){
                //蛇头和蛇身发生碰撞，游戏失败
                GameWin.state = GameState.FAILED;
            }
        }
        //第一节蛇身
        bodyObjList.get(0).x = x;
        bodyObjList.get(0).y = y;

        //蛇头移动
        switch (direction) {
            case "up" -> y -= height;
            case "down" -> y += height;
            case "left" -> x -= width;
            case "right" -> x += width;
        }
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);

        //获取食物
        FoodObj foodObj = gameWin.foodObj;

        Integer newX = null;
        Integer newY = null;
        if(GameUtils.isCollision(this,foodObj)){//吃掉食物
            this.gameWin.foodObj = foodObj.getFood();

            //记录下最后一节蛇身的坐标
            int bodySize = this.gameWin.bodyObjList.size();
            BodyObj bodyObj = this.gameWin.bodyObjList.get(bodySize-1);
            newX = bodyObj.x;
            newY = bodyObj.y;

            //分数+1
            GameWin.score++;
        }

        //通关判断
        if (GameWin.score >= GameUtils.COMPLETE_SCORE) {
            GameWin.state = GameState.COMPLETED;
        }

        move();

        //蛇移动结束后，判断是否需要增加一节蛇身，增加蛇身就是bodyObjList.add()
        if(newX!=null){
            this.gameWin.bodyObjList.add(new BodyObj(newX,newY,gameWin));
        }




        //越界处理
        if (x < 0) {
            x = 570;
        } else if (x > 570) {
            x = 0;
        } else if (y < 30) {//此处是因为有标题栏，所以不能小于0
            y = 570;
        } else if (y > 570) {
            y = 0;
        }
    }
}
